package org.scboy.dbms.service.imp;

import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.InterCodeDao;
import org.scboy.dbms.dao.InterLangDao;
import org.scboy.dbms.dao.InterMsgDao;
import org.scboy.dbms.loads.LoadInter;
import org.scboy.dbms.service.InterCodeService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.MD5;

@Service
public class InterCodeServiceImpl implements InterCodeService {

	@Autowired
	EntityFactory factory;
	
	@Autowired
	LoadInter cache;
	
	/**
	 * 分页查询语言
	 * @param model 条件集合
	 * @return
	 */
   public Page page(Model model) {
	   InterCodeDao dao=factory.getDao(InterCodeDao.class);
	   return dao.page(model);
   }
   
   /**
	 * 获取语言信息
	 * @param code 主键。
	 * @return
	 */
   public Model get(String id) {
	   InterCodeDao dao=factory.getDao(InterCodeDao.class);
	   InterMsgDao msgDao=factory.getDao(InterMsgDao.class);
	   Model data=dao.get(Model.create().add("id", id));
	   
	   List<Model> msgs= msgDao.queryList(Model.create().add("id", id));
	   for (Model msg : msgs) {
		   data.add(msg.getString("lang"), msg.getString("msg"));
	   }
	   return data;
   }
   
   /**
 	 * 新增语言
 	 * @param model 参数集合
 	 * @return
 	 */
   public boolean add(Model model) {
	   InterCodeDao dao=factory.getDao(InterCodeDao.class);
	   InterLangDao langDao=factory.getDao(InterLangDao.class);
	   InterMsgDao msgDao=factory.getDao(InterMsgDao.class);

	   String codeId=MD5.create().digestHex(model.getString("appName")+"-"+model.getString("code"));
	   
	   if(dao.count(Model.create().add("id", codeId))>0) {
		   return false;
	   }
	   
	   for (Model lang : langDao.select(Model.create())) {
		   Model msg=Model.create();
		   msg.add("id", UUID.randomUUID().toString());
		   msg.add("codeId", codeId);
		   msg.add("lang", lang.getString("lang"));
		   msg.add("msg", model.getString(lang.getString("lang")));
		   msgDao.insert(msg);
	   }
	   
	   model.add("id", codeId);
	   int count=dao.insert(model);
	   if(count>0) {
		   cache.removeAll();
		   return true;
	   }else {
		   return false;
	   }
   }
   
   /**
	 * 更新语言
	 * @param model 参数集合
	 * @return
	 */
   public boolean update(Model model) {
	   InterCodeDao dao=factory.getDao(InterCodeDao.class);
	   InterLangDao langDao=factory.getDao(InterLangDao.class);
	   InterMsgDao msgDao=factory.getDao(InterMsgDao.class);
	   for (Model lang : langDao.select(Model.create())) {
		   Model msg=Model.create();
		   msg.add("id", UUID.randomUUID().toString());
		   msg.add("codeId", model.getString("id"));
		   msg.add("lang", lang.getString("lang"));
		   msg.add("msg", model.getString(lang.getString("lang")));
		   msgDao.insert(msg);
	   }
	   int count=dao.update(model);
	   if(count>0) {
		   cache.removeAll();
		   return true;
	   }else {
		   return false;
	   }
   }
   
   /**
    * 删除服务端国际化
    * @param code 主键。
    * @return
    */
   @Transactional
   public  boolean delete(String id) {
	   InterCodeDao dao=factory.getDao(InterCodeDao.class);
	   InterMsgDao msgDao=factory.getDao(InterMsgDao.class);
	   msgDao.delete(Model.create().add("codeId", id));
	   int count=dao.delete(Model.create().add("id", id));
	   if(count>0) {
		   cache.removeAll();
		   return true;
	   }else {
		   return false;
	   }
   }
   
}
